import java.util.ArrayList;
import java.util.Scanner;

public class room {
    int x;
    int y;
    String input;
    Scanner in = new Scanner(System.in);
    ArrayList<String> exits = new ArrayList<String>();
    ArrayList<monster> monsters = new ArrayList<monster>();
    boolean monsterExists;
    monster mob;

    public void run(player p, ArrayList<room> rooms){
        System.out.printf("\n\nyou are in %d,%d\n", this.x, this.y);

        if (monsterExists) {
            if (mob.health > 0) {
                System.out.printf("A %s is in the room!", mob.type);
                combat(mob,p);
            } else {
                System.out.printf("There is a dead %s laying on the floor.", mob.type);
            }
        } else {
            System.out.println("There is a chest in the room.");
        }

        System.out.println("Where would you like to go?:");
        for (int i = 0; i < exits.size(); i++) {
            System.out.println(exits.get(i));
        }
        
        input = in.nextLine();
        int exitx = this.x;
        int exity = this.y;

        if ((input.equals("north")) && (exits.contains(input))) {
            exity++;
        } else if ((input.equals("south")) && (exits.contains(input))) {
            exity--;
        } else if ((input.equals("east"))  && (exits.contains(input))) {
            exitx++;
        } else if((input.equals("west")) && (exits.contains(input))) {
            exitx--;
        } else {
            System.out.println("That is not a valid room");
        }

        for (int i = 0; i < rooms.size(); i++) {
            if ((rooms.get(i).x == exitx) && (rooms.get(i).y == exity)) {
                rooms.get(i).run(p,rooms);
            } else { }
        }

    }

    void combat(monster monster, player p){
        while (monster.health > 1) {
            int damage = p.attack + ((int) (Math.random() * 5));

            System.out.printf("You attack the %s for %d damage.\n" ,monster.type, damage);
            monster.health = monster.health - damage;
            
            if (monster.health < 0){
                monster.health = 0;
            } else { }
            
            System.out.printf("%s health: %d\n" ,monster.type, monster.health);
            
        }
        System.out.printf("the %s is dead.\n" ,monster.type);
    }

    public void setcoordinates(int x, int y, int size) {

        int number = (int) (Math.random() * 100);

        if (number > 30) {
            monsterExists = true;

            monster ogre = new monster();
            ogre.setstats(5,35,"ogre");
            monsters.add(ogre);

            monster wolf = new monster();
            wolf.setstats(6,15,"wolf");
            monsters.add(wolf);

            monster rat = new monster();
            rat.setstats(4,10,"rat");
            monsters.add(rat);

            int monsterPick = (int) (Math.random() * (monsters.size()));
            mob = monsters.get(monsterPick);
        } else {
            monsterExists = false;
            //this is where you would set the loot.
        }

        this.x = x;
        this.y = y;

        if (this.x - 1 < 0) {
        } else { 
            exits.add("west");
        }
        if ((this.x + 1) > (size - 1)) {
        } else {
            exits.add("east");
        }
        if (this.y - 1 < 0) {
        } else {
            exits.add("south");
        }
        if (this.y + 1 > (size - 1)) {
        } else {
            exits.add("north");
        }
    }
}
